<template>
  <view class="cart-container" v-if="getCartList.length > 0">
    <!-- 地址栏 -->
    <my-address />
    <!-- 购物车商品列表的标题区域 -->
    <view class="cart-title">
      <!-- 左侧的图标 -->
      <uni-icons type="shop" size="18"></uni-icons>
      <!-- 描述文本 -->
      <text class="cart-title-text">购物车</text>
    </view>
    <!-- my-goods商品条列 -->
    <uni-swipe-action>
      <uni-swipe-action-item
        :right-options="options"
        v-for="i in getCartList"
        :key="i.goods_id"
        @click="del(i.goods_id)"
      >
        <my-goods :goods="i">
          <template #radio>
            <radio
              @click.stop="changeState(i.goods_id, !i.goods_state)"
              :checked="i.goods_state"
              color="#c00000"
            />
          </template>
          <template #num>
            <!-- 当自己节点不能阻止冒泡时候，可以在父节点中 -->
            <view @click.stop="">
              <uni-number-box
                @change="changeNum(i.goods_id, $event)"
                v-model="i.goods_number"
                :min="1"
              ></uni-number-box>
            </view>
          </template>
        </my-goods>
      </uni-swipe-action-item>
    </uni-swipe-action>
    <!-- 结算区域 -->
    <my-settle />
  </view>
  <view class="empty-cart" v-else>
    <image src="/static/cart_empty@2x.png" class="empty-img"></image>
    <text class="tip-text">空空如也~</text>
  </view>
</template>

<script>
import tarBarBadge from '@/mixins/tarBar-badge.js'
import { mapGetters, mapMutations } from 'vuex'
export default {
  mixins: [tarBarBadge],
  data () {
    return {
      options: [
        {
          text: '删除',
          style: {
            backgroundColor: '#c00000'
          }
        }
      ]
    }
  },
  computed: {
    ...mapGetters('cart', ['getCartList'])
  },
  methods: {
    ...mapMutations('cart', [
      'updateGoodState',
      'updateGoodNum',
      'deleteGoods'
    ]),
    // 选中状态的改变
    changeState (goods_id, goods_state) {
      // console.log(goods_id, goods_state)
      this.updateGoodState({ goods_id, goods_state })
    },
    // 数量的改变
    changeNum (goods_id, goods_number) {
      this.updateGoodNum({ goods_id, goods_number })
    },
    // 删除项
    del (goods_id) {
      this.deleteGoods(goods_id)
    }
  }
}
</script>

<style lang="scss" scoped>
.cart-container {
  padding-bottom: 50px;
  .cart-title {
    height: 40px;
    display: flex;
    align-items: center;
    font-size: 14px;
    padding-left: 5px;
    border-bottom: 1px solid #efefef;
    .cart-title-text {
      margin-left: 10px;
    }
  }
}
.empty-cart {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-top: 150px;

  .empty-img {
    width: 90px;
    height: 90px;
  }

  .tip-text {
    font-size: 12px;
    color: gray;
    margin-top: 15px;
  }
}
</style>
